<?php
//RoSPORA V1.6 RC1 PHP&MYSQL DATA FILE
require_once( 'config.php' );
require_once( 'languages.php' );
//Map info
$maps_a = Array(
		0 => 'Azeroth',
		1 => 'Kalimdor',
		2 => 'UnderMine',
		13 => 'Test zone',
		17 => 'Kalidar',
		30 => 'Alterac Valley',
		33 => 'Shadowfang Keep Instance',
		34 => 'The Stockade Instance',
		35 => 'Stormwind Prizon',
		36 => 'Deadmines Instance',
		37 => 'Plains of Snow',
		43 => 'Wailing Caverns Instance',
		44 => 'Monastery Interior',
		47 => 'Razorfen Kraul Instance',
		48 => 'Blackfathom Deeps Instance',
		70 => 'Uldaman Instance',
		90 => 'Gnomeregan Instance',
		109 => 'Sunken Temple Instance',
		129 => 'Razorfen Downs Instance',
		530 => 'Outland',
		169 => 'Emerald Forest',
		189 => 'Scarlet Monastery Instance',
		209 => 'Zul\'Farrak Instance',
		229 => 'Blackrock Spire Instance',
		230 => 'Blackrock Depths Instance',
		249 => 'Onyxias Lair Instance',
		269 => 'Caverns of Time',
		289 => 'Scholomance Instance',
		309 => 'Zul\'Gurub Instance',
		329 => 'Stratholme Instance',
		349 => 'Maraudon Instance',
		369 => 'Deeprun Tram',
		389 => 'Ragefire Chasm Instance',
		409 => 'The Molten Core Instance',
		429 => 'Dire Maul Instance',
		449 => 'Alliance PVP Barracks',
		450 => 'Horde PVP Barracks',
		451 => 'Development Land',
		469 => 'Blackwing Lair Instance',
		489 => 'Warsong Gulch',
		509 => 'Ruins of AhnQiraj Instance',
		529 => 'Arathi Basin',
		531 => 'Temple of AhnQiraj Instance',
		533 => 'Naxxramas Instance',
		532 => 'Karazahn',
		534 => 'Hyjal Past',
		540 => 'Shattered Halls Instance',
		542 => 'Blood Furnace Instance',
		543 => 'Hellfire Ramparts Instance',
		544 => 'Magtheridons Lair Instance',
		545 => 'Steamvault Instance',
		546 => 'Underbog Instance',
		547 => 'Slave Pens Instance',
		548 => 'Coilfang Resevoir Instance',
		550 => 'Tempest Keep',
		552 => 'Tempest Keep',
		553 => 'Tempest Keep',
		554 => 'Tempest Keep',
		555 => 'Auchindoun',
		556 => 'Auchindoun',
		557 => 'Auchindoun',
		558 => 'Auchindoun',
		559 => 'Nagrand Arena',
		560 => 'Hillsbrad Past',
		562 => 'Blades Edge Arena',
		564 => 'Black Temple',
		565 => 'Gruuls Lair',
		566 => 'Netherstorm Arena',
		568 => 'Zul\'Aman',
);
//Zone info
$zone = Array(
	0 => Array(
		Array(700,10,1244,1873,'Undercity',1497),
		Array(-840,-1330,-5050,-4560,'Ironforge',1537),
		Array(1190,200,-9074,-8280,'Stormwind City',1519),
		Array(-2170,-4400,-7348,-6006,'Badlands',3),
		Array(-500,-4400,-4485,-2367,'Wetlands',11),
		Array(2220,-2250,-15422,-11299,'Stranglethorn Vale',33),
		Array(-1724,-3540,-9918,-8667,'Redridge Mountains',44),
		Array(-2480,-4400,-6006,-4485,'Loch Modan',38),
		Array(662,-1638,-11299,-9990,'Duskwood',10),
		Array(-1638,-2344,-11299,-9918,'Deadwind Pass',41),
		Array(834,-1724,-9990,-8526,'Elwynn Forest',12),
		Array(-500,-3100,-8667,-7348,'Burning Steppes',46),
		Array(-608,-2170,-7348,-6285,'Searing Gorge',51),
		Array(2000,-2480,-6612,-4485,'Dun Morogh',1),
		Array(-1575,-5425,-432,805,'The Hinterlands',47),
		Array(3016,662,-11299,-9400,'Westfall',40),
		Array(600,-1575,-1874,220,'Hillsbrad Foothills',267),
		Array(-2725,-6056,805,3800,'Eastern Plaguelands',139),
		Array(-850,-2725,805,3400,'Western Plaguelands',28),
		Array(2200,600,-900,1525,'Silverpine Forest',130),
		Array(2200,-850,1525,3400,'Tirisfal Glades',85),
		Array(-2250,-3520,-12800,-10666,'Blasted Lands',4),
		Array(-2344,-4516,-11070,-9600,'Swamp of Sorrows',8),
		Array(-1575,-3900,-2367,-432,'Arathi Highlands',45),
		Array(600,-1575,220,1525,'Alterac Mountains',36),
	),
	1 => Array(
		Array(2698,2030,9575,10267,'Darnassus',1657),
		Array(326,-360,-1490,-910,'Thunder Bluff',1638),
		Array(-3849,-4809,1387,2222,'Orgrimmar',1637),
		Array(-1300,-3250,7142,8500,'Moonglade',493),
		Array(2021,-400,-9000,-6016,'Silithus',1377),
		Array(-2259,-7000,4150,8500,'Winterspring',618),
		Array(-400,-2094,-8221,-6016,'Un\'Goro Crater',490),
		Array(-590,-2259,3580,7142,'Felwood',361),
		Array(-3787,-8000,1370,6000,'Azshara',16),
		Array(-1900,-5500,-10475,-6825,'Tanaris',440),
		Array(-2478,-5500,-5135,-2330,'Dustwallow Marsh',15),
		Array(360,-1536,-3474,-412,'Mulgore',215),
		Array(4000,-804,-6828,-2477,'Feralas',357),
		Array(3500,360,-2477,372,'Desolace',405),
		Array(-804,-5500,-6828,-4566,'Thousand Needles',400),
		Array(-3758,-5500,-1300,1370,'Durotar',14),
		Array(1000,-3787,1370,4150,'Ashenvale',331),
		Array(2500,-1300,4150,8500,'Darkshore',148),
		Array(3814,-1100,8600,11831,'Teldrassil',141),
		Array(3500,-804,-412,3580,'Stonetalon Mountains',406),
		Array(-804,-4200,-4566,1370,'The Barrens',17),
	),
	530 => Array(
		Array(6135.25,4829,-2344.78,-1473.95,'Shattrath City',3703),
		Array(-6400.75,-7612.20,9346.93,10153.70,'Silvermoon City',3487),
		Array(5483.33,-91.66,1739.58,5456.25,'Netherstorm',3523),
		Array(7083.33,1683.33,-4600,-999.99,'Terokkar Forest',3519),
		Array(10295.83,4770.83,-3641.66,41.66,'Nagrand',3518),
		Array(-10075,-13337.49,-2933.33,-758.33,'Bloodmyst Isle',3525),
		Array(8845.83,3420.83,791.66,4408.33,'Blades Edge Mountains',3522),
		Array(4225,-1275,-5614.58,-1947.91,'Shadowmoon Valley',3520),
		Array(-11066.36,-12123.13,-4314.37,-3609.68,'The Exodar',3557),
		Array(9475,4447.91,-1416.66,1935.41,'Zangarmarsh',3521),
		Array(5539.58,375,-1962.49,1481.25,'Hellfire Peninsula',3483),
		Array(-10500,-14570.83,-5508.33,-2793.75,'Azuremyst Isle',3524),
		Array(-5283.33,-8583.33,6066.66,8266.66,'Ghostlands',3433),
		Array(-4487,-9412,7758,11041,'Eversong Woods',3430)
	),
);
//Define characters
$def = Array(
	'character_race' => Array(
		1 => 'Human',
		2 => 'Orc',
		3 => 'Dwarf',
		4 => 'Night Elf',
		5 => 'Undead',
		6 => 'Tauren',
		7 => 'Gnome',
		8 => 'Troll',
		9 => 'Goblin',
        10 => 'Blood Elf',
        11 => 'Dranei',
	),
//Define classes
	'character_class' => Array(
		1 => 'Warrior',
		2 => 'Paladin',
		3 => 'Hunter',
		4 => 'Rogue',
		5 => 'Priest',
		7 => 'Shaman',
		8 => 'Mage',
		9 => 'Warlock',
		11 => 'Druid',
	),
);
//DATABASE LAYER - HIGH PRIORITY CODES
class DBLayer
{
	var $link_id;
	var $query_result;
	var $saved_queries = array();
	var $num_queries = 0;

		function error()
	{
		$result['error_sql'] = @current(@end($this->saved_queries));
		$result['error_no'] = @mysql_errno($this->link_id);
		$result['error_msg'] = @mysql_error($this->link_id);

		return $result;
	}
	
	
	function DBLayer($host, $user, $password, $db)
	{
		$this->link_id = mysql_connect($host, $user, $password, true);

		if ($this->link_id)
		{
			if (mysql_select_db($db, $this->link_id))
				return $this->link_id;
			else
				error('Unable to select database.');
		}
		else
			error('Unable to connect to MySQL server.');
			mysql_close($this);
	}

	function query($sql)
	{
		$this->query_result = @mysql_query($sql, $this->link_id);

		if ($this->query_result)
		{
			++$this->num_queries;
			return $this->query_result;
		}
		else
		{
			return false;
		}
		mysql_close($this);
	}


	function result($query_id = 0, $row = 0)
	{
		return ($query_id) ? @mysql_result($query_id, $row) : false;
	}


	function fetch_assoc($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
	}


	function fetch_row($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_row($query_id) : false;
	}
	
	
	function fetch_object($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_object($query_id) : false;
	}
	

	function num_rows($query_id = 0)
	{
		return ($query_id) ? @mysql_num_rows($query_id) : false;
	}


	function affected_rows()
	{
		return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
	}


	function insert_id()
	{
		return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
	}


	function get_num_queries()
	{
		return $this->num_queries;
	}


	function get_saved_queries()
	{
		return $this->saved_queries;
	}


	function free_result($query_id = false)
	{
		return ($query_id) ? @mysql_free_result($query_id) : false;
	}


	function escape($str)
	{
		if (function_exists('mysql_real_escape_string'))
			return mysql_real_escape_string($str, $this->link_id);
		else
			return mysql_escape_string($str);
	}


	function close()
	{
		if ($this->link_id)
		{
			if ($this->query_result)
				@mysql_free_result($this->query_result);

			return @mysql_close($this->link_id);
		}
		else
			return false;
			
	}
}
//Start creation of accounts and verify
function check_email_address($email) {
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    return false;
  }
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
      return false;
    }
  }
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
      return false;
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
function check_for_symbols($string){
  $len=strlen($string);
  $alowed_chars="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for($i=0;$i<$len;$i++)
    if(!strstr($alowed_chars,$string[$i]))
       return TRUE;
  return FALSE;
}
function sha_password($user,$pass){
  $user = strtoupper($user);
  $pass = strtoupper($pass);
  return SHA1($user.':'.$pass);
}
//
//Start realm works
function test_realm(){
	global $server, $port;
	$s = @fsockopen("$server", $port, $ERROR_NO, $ERROR_STR,(float)0.5);
	if($s){@fclose($s);return true;} else return false;
}
function get_realm_name(){
	global $hostr, $userr, $passwordr, $dbr, $database_encoding;
	$realm_db = new DBLayer($hostr, $userr, $passwordr, $dbr);
	$realm_db->query("SET NAMES $database_encoding");
	$query = $realm_db->query("SELECT * FROM `realmlist` WHERE id = '1'");
	$result = $realm_db->fetch_assoc($query);
	$realm_db->close();
	unset($realm_db);
	return($result['name']);
}
$title=get_realm_name();
$copyright=get_realm_name();
$status=get_realm_name();
function get_realm_data(){
	global $hostr, $userr, $passwordr, $dbr, $database_encoding;
	$realm_db = new DBLayer($hostr, $userr, $passwordr, $dbr);
	$realm_db->query("SET NAMES $database_encoding");
	$query = $realm_db->query("SELECT * FROM `realmlist`");
	$result = $realm_db->fetch_assoc($query);
	$realm_db->close();
	unset($realm_db);
	$result['name']=$result['name']." Server";
	return($result);
}
//
//Rospora credits
$credits = "<left><a href=\"http://rospora.powerlogy.net/\">RoSPORA</a> is a free software released under the <a href=\"http://www.gnu.org/copyleft/gpl.html\">GNU/GPL</a><br>This <a href=\"http://rospora.powerlogy.net/\">RoSPORA V1.6 RC1</a> licenced to $title</left>";
//Online player works
	function make_players_arrayrealm(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding, $pl_array;
  	$i=0;
	$mangos_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$mangos_db->query("SET NAMES $database_encoding");
	$query = $mangos_db->query("SELECT * FROM `characters` WHERE `online`='1' ORDER BY `name`");
	while($result = $mangos_db->fetch_assoc($query))
	{
        $memberid = $result["guid"];
		$char_data = explode(' ',$result['data']);
		$char_gender = dechex($char_data[36]);
		$char_gender = str_pad($char_gender,8, 0, STR_PAD_LEFT);
		$char_gender = $char_gender{3};
		$guild = get_guildname($result['guid']);
		$res_pos=get_zone_name($result['map'], $result['position_x'], $result['position_y']);
		if ($result['race'] == 1 || $result['race'] == 3 || $result['race'] == 4 || $result['race'] == 7 || $result['race'] == 11) { $char_side=1; } else { $char_side=2; };
	$pl_array[$i] = Array($result['name'], $result['race'], $result['class'], $char_data[34], $res_pos, $rank, $char_gender, $char_side, $rank, $guild, $result['guid']);
	$i++;
	}
	$mangos_db->close();
	unset($mangos_db);
	return $i;
	}
	$onlineplayers=make_players_arrayrealm();

		if (!$sort = &$_GET['s']) $sort=0;
		if (!$flag = &$_GET['f']) $flag=0;
		if ($flag==0){	
		$flag=1; $sort_type='<'; 
		}
		else{
		$flag=0; $sort_type='>'; 
		}
		$link=$_SERVER['PHP_SELF']."?f=".$flag."&s=";

		if (!empty($pl_array))
		{
		usort($pl_array, create_function('$a, $b', 'if ( $a['.$sort.'] == $b['.$sort.'] ) return 0; if ( $a['.$sort.'] '.$sort_type.' $b['.$sort.'] ) return -1; return 1;'));
		}
	$listone="";
	$i=0;
	while ($i < $onlineplayers)
	{
        $member=$pl_array[$i][6];
	$name=$pl_array[$i][0];
	$race=$pl_array[$i][1];
	$class=$pl_array[$i][2];
	$res_race = $def['character_race'][$race];
	$res_class = $def['character_class'][$class];
	$lvl=$pl_array[$i][3];
	$loc=$pl_array[$i][4];
	$gender=$pl_array[$i][5];
	if ($pl_array[$i][6]==1) {$side='alliance';} else { $side='horde'; };
	$res_side = $def['character_side'][$pl_array[$i][6]];
	$rank = $pl_array[$i][7];
	$res_rank = $def['character_rank'][$side][$rank];
	if ($rank==1) {
    $rankshow='A';
	 }
    else $rankshow='H';
	$guild = $pl_array[$i][8];
	$guildname=$gl_array[$i][0];
    	$guildleader=$gl_array[$i][1];
    	$guildmembers=$gl_array[$i][2];
	if($guild=="") $guild="None";
	$listone.= "<tr class='realms'>
					<td class='realms'></td>
					<td class='realms' align='center'>$rankshow</td>
					<td class='realms'>$name</td>
					<td class='realms' align='center'>$lvl</td>
					<td class='realms'>$guild</td>
					<td class='realms' align='center'>$res_race</td>
					<td class='realms' align='center'>$res_class</td>					
					<td class='realms'>$loc</td>
				</tr>";
	$i++;
	}
//
//Exclude character's zone
function get_zone_name($mapid, $x, $y){
global $maps_a, $zone;

if (!empty($maps_a[$mapid]))
	{
	$zmap=$maps_a[$mapid];
	if (($mapid==0) or ($mapid==1) or ($mapid==530))
		{
		$i=0; $c=count($zone[$mapid]);
		while ($i<$c)
			{
	if ($zone[$mapid][$i][2] < $x  AND $zone[$mapid][$i][3] > $x AND $zone[$mapid][$i][1] < $y  AND $zone[$mapid][$i][0] > $y) $zmap=$zone[$mapid][$i][4];
			$i++;
			}
		}
	} else $zmap="Unknown zone";
return $zmap;
    } 
	function get_map_name($mapid)
{
  global $maps_a;
  if (!empty($maps_a[$mapid]))
  {
    $zmap=$maps_a[$mapid];
	} else $zmap="Unknown";
  return $zmap;
}
//
//Get server data's
function max_users(){
	global $host, $user, $password, $db, $database_encoding;
	$mangos_db = new DBLayer($host, $user, $password, $db);
	$mangos_db->query("SET NAMES $database_encoding");
	$query = $mangos_db->query("SELECT maxplayers FROM `uptime` ORDER BY maxplayers DESC");
  $result = $mangos_db->fetch_assoc($query);
  $mangos_db->close();
	unset($mangos_db);
	return($result['maxplayers']);
}
$time=max_users();
function max_char(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT * FROM `characters`");
    $result = $character_db->num_rows($query);
  $character_db->close();
	unset($character_db);
	return($result);
}
$max_char=max_char();
function max_acc(){
	global $hostr, $userr, $passwordr, $dbr, $database_encoding;
	$realm_db = new DBLayer($hostr, $userr, $passwordr, $dbr);
	$realm_db->query("SET NAMES $database_encoding");
	$query = $realm_db->query("SELECT * FROM `account`");
@	$result = $realm_db->num_rows($query);
	$realm_db->close();
	unset($realm_db);
	return($result);
}
$max_acc=max_acc();
function max_gm(){
	global $hostr, $userr, $passwordr, $dbr, $database_encoding;
	$realm_db = new DBLayer($hostr, $userr, $passwordr, $dbr);
	$realm_db->query("SET NAMES $database_encoding");
	$query = $realm_db->query("SELECT * FROM `account` WHERE gmlevel>'0'");
@	$result = $realm_db->num_rows($query);
	$realm_db->close();
	unset($realm_db);
	return($result);
}
$max_gm=max_gm();
function get_db_rev(){
	global $host, $user, $password, $db, $database_encoding;
	$mangos_db = new DBLayer($host, $user, $password, $db);
	$mangos_db->query("SET NAMES $database_encoding");
	$query = $mangos_db->query("SELECT * FROM `db_version`");
	$result = $mangos_db->fetch_assoc($query);
	$mangos_db->close();
	unset($mangos_db);
	return($result['version']);
}
$rev=get_db_rev();
//
//Statics works starts
//Alliance
function max_human(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='1'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_human=max_human();

function max_dwarf(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='3'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_dwarf=max_dwarf();

function max_nightelf(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='4'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_nightelf=max_nightelf();

function max_gnome(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='7'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_gnome=max_gnome();

function max_dranei(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='11'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_dranei=max_dranei();

//Horde
function max_orc(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='2'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_orc=max_orc();

function max_undead(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='5'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_undead=max_undead();

function max_tauren(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='6'");
  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_tauren=max_tauren();

function max_troll(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='8'");
@  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_troll=max_troll();

function max_bloodelf(){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT COUNT(*) AS race FROM `characters` WHERE race='10'");
@  $result = $character_db->fetch_assoc($query);
  $character_db->close();
	unset($character_db);
	return($result['race']);
}
$max_bloodelf=max_bloodelf();
//
//Percent system
$staticpic = "percent.php?per=";
@$yuzde_human=round($max_human*100/$max_char);
@$yuzde_dwarf=round($max_dwarf*100/$max_char);
@$yuzde_nightelf=round($max_nightelf*100/$max_char);
@$yuzde_gnome=round($max_gnome*100/$max_char);
@$yuzde_dranei=round($max_dranei*100/$max_char);
@$yuzde_orc=round($max_orc*100/$max_char);
@$yuzde_troll=round($max_troll*100/$max_char);
@$yuzde_undead=round($max_undead*100/$max_char);
@$yuzde_tauren=round($max_tauren*100/$max_char);
@$yuzde_bloodelf=round($max_bloodelf*100/$max_char);
//
//Get guild name
function get_guildname($idplayer){
	global $hostc, $userc, $passwordc, $dbc, $database_encoding;
	$character_db = new DBLayer($hostc, $userc, $passwordc, $dbc);
	$character_db->query("SET NAMES $database_encoding");
	$query = $character_db->query("SELECT g.name FROM guild g, guild_member gm WHERE gm.guid=guid");
	$result = $character_db->fetch_assoc($query);
	$character_db->close();
	unset($character_db);
	return($result['name']);
	$guild_name = $result['name'];
}
//Account creation page and verify
  if (empty($_POST['username']) and empty($_POST['passw']) and empty($_POST['email']))
	{
	$co="'";
	$cont='<form id="form1" name="form1" method="post" action="">
  <tr class="acc">
    <th scope="row" class="acc">'.$text["name"].'</th>
    <td class="acc" scope="col"><input type="text" name="username" />    </td>
  </tr>
  <tr class="acc">
    <th scope="row" class="acc">'.$text["password"].'</th>
    <td class="acc"><input name="passw" type="password" id="passw"/></td>
  </tr>
  <tr class="acc">
    <th scope="row" class="acc">'.$text["seccode"].'</th>
    <td class="acc"><IMG src="captcha.php"></td>
  </tr>
  <tr class="acc">
    <th scope="row" class="acc">'.$text["seccode2"].'</th>
    <td class="acc"><input name="txtNumber" type="text" id="txtNumber" size="5" maxlength="5" /></td>
  </tr>
  <tr class="acc">
    <th scope="row" class="acc">E-Mail :</th>
    <td class="acc"><input name="email" type="text" id="email" /></td>
  </tr>
  <tr class="acc">
    <th scope="row" class="acc">'.$text["expension"].'</th>
    <td class="acc"><input name="tbc" type="checkbox" id="tbc" value="1" checked/></td>
  </tr>
  <tr class="acc">
    <th scope="row" class="acc">&nbsp;</th>
    <td class="acc"><form id="form2" name="form2" method="post" action="">
      <input type="submit" name="Submit" value="Submit" />
    </form>
';
	}
	elseif  (empty($_POST['username']) or empty($_POST['passw']) or empty($_POST['email']))
	{

	$cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL class=error>'.$text["not_all"].'<br><br><a href="" onClick="history.go(-1)">'.$text["backtext"].'</a></SMALL></TD></TD></TR>';

	}
	elseif (check_for_symbols($_POST['username'])==TRUE or check_for_symbols($_POST['passw'])==TRUE){
	
	$cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL class=error>'.$text["not_simbol"].'<br><br><a href="" onClick="history.go(-1)">'.$text["backtext"].'</a></SMALL></TD></TD></TR>';

	}
	else
	{
	$username = htmlspecialchars(trim("$_POST[username]"));
	$passw = trim($_POST['passw']);
	$email = htmlspecialchars(trim($_POST['email']));
	$tbc = $_POST['tbc'];
	$ip = getenv('REMOTE_ADDR');
	$realm_db = new DBLayer($hostr, $userr, $passwordr, $dbr);
	$realm_db->query("SET NAMES $database_encoding");
$ip_cr=0;
if ($lock_reg!=0){
  $query = $realm_db->query("SELECT `last_ip` FROM `account`  WHERE `last_ip`='$ip'");
  while($result = $realm_db->fetch_assoc($query)) $ip_cr++;
}
if (!check_email_address($_POST['email'])) {
  $cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL class=error>'.$text["email"].'<br><br><a href="" onClick="history.go(-1)">'.$text["backtext"].'</a></SMALL></TD></TD></TR>';
} else {
  session_start();
  $number = strtoupper($_POST['txtNumber']);
  if (md5($number) != $_SESSION['image_random_value']) {
    $cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL class=error>'.$text["key"].'<br><br><a href="" onClick="history.go(-1)">'.$text["backtext"].'</a></SMALL></TD></TD></TR>';
  } else {
    if (($ip_cr>=$lock_reg) && ($lock_reg!=0)) {
      $cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL class=error>'.$text["ip_limit"][0].$ip.'<br>'.$ip_cr.$text["ip_limit"][1].'<br><br><a href="" onClick="history.go(-1)">'.$text["backtext"].'</a></SMALL></TD></TD></TR>';
    } else {
      $sha=sha_password($username,$passw);
      if($tbc==1) {
        if($realm_db->query("INSERT INTO `account` (`username`,`sha_pass_hash`,`email`,`last_ip`,`locked`,`tbc`) VALUES ('$username','$sha','$email','$ip','$lock_acc','$tbc')")) {
          $realm_db->query("UPDATE `account` SET `password`='$passw' WHERE `username`='$username'");
          $cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL>'.$text["acc"].'<br><strong>'.$username.'</strong><br>'.$text["create"].'</SMALL></TD></TD></TR>';
        } else {
          $cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL class=error>'.$text["acc"].'<br><strong>'.$username.'</strong><br>'.$text["failed"].'<br><br><a href="" onClick="history.go(-1)">'.$text["backtext"].'</a></SMALL></TD></TD></TR>';
        }
      } else {
        if($realm_db->query("INSERT INTO `account` (`username`,`sha_pass_hash`,`email`,`last_ip`,`locked`) VALUES ('$username','$sha','$email','$ip','$lock_acc')")) {
          $realm_db->query("UPDATE `account` SET `password`='$passw' WHERE `username`='$username'");
          $cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL>'.$text["acc"].'<br><strong>'.$username.'</strong><br>'.$text["create"].'</SMALL></TD></TD></TR>';
        } else {
          $cont='<TR><TD rowSpan=6><IMG height=152 width=1 src="'.$img_base.'pixel.gif"><TD align=Center><SMALL class=error>'.$text["acc"].'<br><strong>'.$username.'</strong><br>'.$text["failed"].'<br><br><a href="" onClick="history.go(-1)">'.$text["backtext"].'</a></SMALL></TD></TD></TR>';
        }
      }
    }
  }
}
	$realm_db->close();
	unset($realm_db);
	}
$dbrev=get_db_rev();

if (test_realm())
	{
	$title.=(' ('.$onlineplayers.' '.$text["playerson"].')');
	$status = "<img alt=Online src='".$img_base."online.png' height='24' width='24'>";
	} else
	{
	$title.=(' '.$text["off"] );
	$status = "<img alt=Offline src='".$img_base."offline.png' height='24' width='24'>";
	}
?>